﻿using FreeSql;
using Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace DBContext
{
    public class DbConfig
    {
        static DbConfig()
        {
            string dbFileName = AppDomain.CurrentDomain.BaseDirectory + "config.db";
            // SQLitePCL.Batteries.Init();
            if (!File.Exists(dbFileName))
            {
                Logger.Log.Info("create history db file");
                File.Create(dbFileName);
            }
            // Microsoft.Data.Sqlite.SqliteConnection _database = new
            // Microsoft.Data.Sqlite.SqliteConnection($"Data Source={dbFileName}");
            DbContext = new FreeSqlBuilder()
                            //.UseConnectionFactory(DataType.Sqlite, () => _database)
                            //.UseConnectionString(DataType.Sqlite, $"Data Source={dbFileName}")
                            .UseConnectionString(DataType.SqlServer, SysCfg.DBStr)
                            .UseAutoSyncStructure(true)
                            .Build();

            string historyFileName = AppDomain.CurrentDomain.BaseDirectory + "history.db";
            if (!File.Exists(historyFileName))
            {
                Logger.Log.Info("create history db file");
                File.Create(historyFileName);
            }

            HistoryDbContext = new FreeSqlBuilder()
                            //.UseConnectionFactory(DataType.Sqlite, () => _database)
                            .UseConnectionString(DataType.Sqlite, $"Data Source={dbFileName}")
                            .UseAutoSyncStructure(true)
                            .Build();
        }

        public static readonly IFreeSql DbContext;

        public static readonly IFreeSql HistoryDbContext;
    }
}